/****************************************************************************************
 * Copyright (c) 2010~2012 All Rights Reserved by
 * G-Net Integrated Service Co., Ltd. 
 ****************************************************************************************/
package com.kingtone.rongji;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.kingtone.rongji.biz.Address;
import com.kingtone.rongji.tool.AppContasts;

/**
 * @Title: StoreMapListActivity.java
 * @author: wenhui.li
 * @date: 2015-4-24 下午2:32:36
 */
public class StoreMapListActivity extends BaseActivity {
	private static final String TAG = "StoreMapListActivity";
	private Activity mContext = StoreMapListActivity.this;
	// private Toast mToast;
	public MapView mMapView = null; // MapView 是地图主控件
	BaiduMap mBaiduMap;

	public static int poiIndex = 0;
	private ArrayList<Address> list = null;

	BitmapDescriptor bdA = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_gcoding);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_map_list);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomTo(12));

		list = (ArrayList<Address>) getIntent().getSerializableExtra(
				"storeList");
		if (list != null) {
			int size = list.size();
			for (int i = 0; i < size; i++) {
				Address add = list.get(i);
				double lat = add.getLatitude();
				double lng = add.getLongitude();
				LatLng llA = new LatLng(lat, lng);
				OverlayOptions ooA = new MarkerOptions().position(llA)
						.icon(bdA).zIndex(9);
				Marker marker = (Marker) mBaiduMap.addOverlay(ooA);
				Bundle bundle = new Bundle();
				bundle.putString(AppContasts.STORE_NAME, add.getShipTo());
				bundle.putString(AppContasts.STORE_ADDRESS, add.getAddress());
				marker.setExtraInfo(bundle);
				if (i == 0) {
					mBaiduMap.setMapStatus(MapStatusUpdateFactory
							.newLatLng(llA));
				}
			}
		}

		mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
			@Override
			public boolean onMarkerClick(Marker marker) {
				Bundle bundle = marker.getExtraInfo();
				if(bundle == null)
					return false;
				
				final String name = bundle.getString(AppContasts.STORE_NAME);
				final String address = bundle.getString(AppContasts.STORE_ADDRESS);
				
				View popview = LayoutInflater.from(mContext).inflate(R.layout.map_popup, null);
				popview.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent();
						intent.putExtra(AppContasts.STORE_NAME, name);
						intent.putExtra(AppContasts.STORE_ADDRESS, address);
						setResult(RESULT_OK,intent);
						finish();
					}
				});
				TextView storeName = (TextView) popview.findViewById(R.id.map_store_name);
				TextView storeAdd = (TextView) popview.findViewById(R.id.map_store_address);
				
				storeName.setText(name);
				storeAdd.setText(address);
				
				LatLng ll = marker.getPosition();
				mBaiduMap.showInfoWindow(new InfoWindow(popview, ll, -47));
				
				return true;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mMapView.onDestroy();
	}

	public void onBackPressed() {
		finish();
	}
}
